﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Newtonsoft.Json;

namespace BrightcoveAPI
{  
    // <summary>
    // The CuePoint object is a marker set at a precise time point in the duration of a video. 
    // You can use cue points to trigger mid-roll ads or to separate chapters or scenes in a long-form video
    // </summary>      
    public class BCCuePoint : BCObject
    {
        #region Private Properties

        private String _name;
        private long _time;
        private BCCuePointType _type;

        #endregion Private Properties

        #region Public Properties

        // <summary>
        // A name for the cue point, so that you can refer to it.
        // </summary> 
        public String name { get { return _name; } }

        // <summary>
        // A list of the ids of one or more videos that this cue point applies to. 
        // </summary> 
        public List<long> videoIds = new List<long>();

        // <summary>
        // The time of the cue point, measured in milliseconds from the beginning of the video.
        // </summary> 
        public long time { get { return _time; } }

        // <summary>
        // If true, the video stops playback at the cue point. This setting is valid only for AD type cue points.
        // </summary> 
        public Boolean forceStop { get; set; }

        // <summary>
        // the type of cue point. One of 0 (AD), 1 (CODE), or 2 (CHAPTER). 
        // An AD cue point is used to trigger mid-roll ad requests. 
        // A CHAPTER cue point indicates a chapter or scene break in the video. 
        // A CODE cue point causes an event that you can listen for and respond to.
        // </summary> 
        public BCCuePointType type { get { return _type; } }

        // <summary>
        // A string that can be passed along with a CODE cue point.
        // </summary> 
        public String metadata { get; set; }

        #endregion Public Properties

        #region Constructor

        public BCCuePoint(JsonReader reader)
        {
            reader.Read();
            while (reader.TokenType != JsonToken.EndObject)
            {
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    switch (reader.Value.ToString())
                    {
                        case "error":
                            reader.Read();
                            Console.WriteLine(String.Format("Error: {0}", reader.Value));
                            break;

                        case "name":
                            reader.Read();
                            _name = (String)reader.Value;
                            break;

                        case "video-id":
                            reader.Read();
                            videoIds = ExplodeLong((String)reader.Value);
                            break;

                        case "time":
                            reader.Read();
                            _time = (long)reader.Value;
                            break;

                        case "forceStop":
                            reader.Read();
                            forceStop = (Boolean)reader.Value;
                            break;

                        case "type":
                            reader.Read();
                            int itype = (int)reader.Value;
                            switch (itype)
                            {
                                case 0: 
                                    _type = BCCuePointType.AD;
                                    break;

                                case 1:
                                    _type = BCCuePointType.CODE;
                                    break;

                                case 2:
                                    _type = BCCuePointType.CHAPTER;
                                    break;
                            }
                            break;

                        case "metadata":
                            reader.Read();
                            metadata = (String)reader.Value;
                            break;

                        default:
                            reader.Read();
                            break;
                    }
                }
                reader.Read();
            }
            reader.Read();
        }

        #endregion Constructor
    }
}
